﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using MemcachedProviders.Cache;
using System.IO;

namespace Pixysoft.Weblications.Sango.Controller
{
    public class EquipmentImageController
    {
        private const string memcache_key = "EquipmentImageController";

        public static byte[] GetEquipmentImage(int size, int equipmentserialcode)
        {
            string memcachedkey = GetMemcachedKey(size, equipmentserialcode);

            byte[] images = DistCache.Get<byte[]>(memcachedkey);

            if (images != null && images.Length > 0)
            {
                return images;
            }

            string filepath = "~/images/sango/equipment/";
            string filename = string.Format("{0}.jpg", equipmentserialcode);

            string path = HttpContext.Current.Server.MapPath(filepath + filename);
            if (!File.Exists(path))
            {
                return null;
            }
            images = File.ReadAllBytes(path);
            DistCache.Add(memcachedkey, images);
            return images;
        }

        private static string GetMemcachedKey(int size, int equipmentserialcode)
        {
            return memcache_key + size + equipmentserialcode;
        }
    }
}
